'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
	let cDate = new Date()
	if (cDate && cDate.toString().indexOf('UTC') > -1) {
		cDate.setHours(cDate.getHours() + 8)
	}
	let cDay = cDate.getDate(); 
	let appletInfoaData = await db.collection('wmz-applet-info').where({
		day: cDay+""
	}).get()
	appletInfoaData = appletInfoaData.data
	console.log(appletInfoaData.length + "条数据需要更新")
	for (var i in appletInfoaData) {
		let appletInfo = appletInfoaData[i]
		if (appletInfo) {
			let money = 297
			let balance = appletInfo.balance
			if (balance) {
				balance = (balance - money).toFixed(2)
				await db.collection('wmz-applet-info').doc(appletInfo._id).update({
					balance: balance
				})
				await db.collection('wmz-entitlement-balance').add({
					app_id: appletInfo._id,
					shop_id: appletInfo._id,
					create_time: new Date().getTime(),
					money: money,
					name: "在线聊天",
					type: "支出",
					way: "在线聊天"
				})
			}
		}
	}
	return event
};